//
//  MLDetailViewController.m
//  keJiNews
//
//  Created by 魔龙 on 16/5/8.
//  Copyright © 2016年 魔龙. All rights reserved.
//

#import "MLDetailViewController.h"
#import "MLNetworkTools.h"
#import "MLApiManager.h"

@interface MLDetailViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;
/// 新闻内容
@property (nonatomic, copy) NSString *body;
/// 新闻标题
@property (nonatomic, copy) NSString *newsTitle;
/// 新闻时间
@property (nonatomic, copy) NSString *time;

@end

@implementation MLDetailViewController


#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.title = @"新闻详情";
    // 加载webView
    [self loadData];
}


- (void)loadData {
//        NSURL *url = [NSURL URLWithString:self.newsURL];
//        NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        [self.webView loadRequest:request];
//    NSLog(@"%@",self.newsURL);
    
    [[MLNetworkTools sharedManager] GET:self.newsURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        NSString *key = responseObject.keyEnumerator.nextObject;
        // 取出数据
        NSDictionary *result = responseObject[key];
        // 取出body内容
        __block NSString *body = result[@"body"];
        // 取出所有的图片
        NSArray *images = result[@"img"];
        // 循环替换图片数据
        [images enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *ref = obj[@"ref"];
            // 替换字符串
            body = [body stringByReplacingOccurrencesOfString:ref withString:[self imageHTMLWithDict:obj]];
            
        }];
        
        // 替换视频
        NSArray *videos = result[@"video"];
        [videos enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *ref = obj[@"ref"];
            body = [body stringByReplacingOccurrencesOfString:ref withString:[self videoHTMLWithDict:obj]];
        }];
        
        self.body = body;
        self.newsTitle = result[@"title"];
        self.time = [NSString stringWithFormat:@"%@  %@",result[@"ptime"],result[@"source"]];
        // 本地html文件的路径
        NSURL *url = [[NSBundle mainBundle]URLForResource:@"detail.html" withExtension:nil];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"出错了");
    }];
    
     

}


- (NSString *)imageHTMLWithDict:(NSDictionary *)dict {
    NSString *html = [NSString stringWithFormat:@"<img src=\"%@\" width=\"100%%\"  alt=\"%@\"/><span style=\"font-size: 13px;color: dimgrey\">%@</span>",dict[@"src"],dict[@"alt"],dict[@"alt"]];
    return html;
}

- (NSString *)videoHTMLWithDict:(NSDictionary *)dict {
    NSString *html = [NSString stringWithFormat:@"<video width=\"100%%\" controls>"
                      "<source src=\"%@\""
                      " type=\"video/mp4\">"
                      "您的浏览器不支持 HTML5 video 标签。"
                      "</video><span style=\"font-size: 13px;color: dimgrey\">%@</span>",dict[@"url_mp4"],dict[@"alt"]];
    return html;
}

#pragma mark - webview的代理
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *code = [NSString stringWithFormat:@"changeContent('%@','%@','%@')",self.newsTitle,self.time,self.body];
    [webView stringByEvaluatingJavaScriptFromString:code];
}


#pragma mark --------懒加载--------
- (UIWebView *)webView{
    if (!_webView) {
        UIWebView *web = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _webView = web;
    }
    return _webView;
}

@end
